package com.evil.kingdom.handler.timer;

import com.evil.common.core.util.StreamUtil;
import com.evil.kingdom.handler.MyWebSocketHandler;
import com.evil.kingdom.handler.SseHandler;
import com.evil.kingdom.pojo.entity.action.Battlefield;
import com.evil.kingdom.pojo.entity.base.SideName;
import com.evil.kingdom.task.timer.BattlefieldTimerTask;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;

/**
 * 战场 处理器
 *
 * @author liyang
 * @date 2024-06-11 14:36
 */
@Component
@Slf4j
public class BattlefieldTimerTaskHandler extends BaseTimerTaskHandler<BattlefieldTimerTask> {
    public static final Logger battlefieldLogger = LoggerFactory.getLogger("KINGDOM_BATTLEFIELD_LOGGER");

    @Resource
    private SseHandler sseHandler;
    @Resource
    private MyWebSocketHandler myWebSocketHandler;
    @Resource
    private BattleTimerTaskHandler battleTimerTaskHandler;

    public void buildBattlefieldTimerTask(Battlefield battlefield, Consumer<SideName> finish) {
        if (StreamUtil.anyMatch(this.getList(), e -> e.getT().implicate(battlefield))) {
            return;
        }
        BattlefieldTimerTask task = BattlefieldTimerTask.build(battleTimerTaskHandler, battlefield,
                c -> {
                },
                (t, c, r) -> {
                    if (null != r) {
                        battlefieldLogger.info("battlefield|handler|finished|result");
                        this.removeTask(c);
                        finish.accept(t);
                    }
                });
        this.addTask(task);
    }
}
